﻿// CheckModelLibraryDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "CheckModelLibraryDialog.h"
#include "afxdialogex.h"

#include "CheckModelNewDialog.h"
#include "CheckModelEditDialog.h"

// CCheckModelLibraryDialog 对话框

IMPLEMENT_DYNAMIC(CCheckModelLibraryDialog, CDialogEx)

CCheckModelLibraryDialog::CCheckModelLibraryDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_CHECK_MODEL_LIBRARY, pParent)
{

}

CCheckModelLibraryDialog::~CCheckModelLibraryDialog()
{
}

void CCheckModelLibraryDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_MFCPROPERTYGRID1, m_grid);
	DDX_Control(pDX, IDC_LIST_MODEL, m_list);
}


BEGIN_MESSAGE_MAP(CCheckModelLibraryDialog, CDialogEx)
	ON_COMMAND(ID_CHECK_MODEL_NEW, &CCheckModelLibraryDialog::OnCheckModelNew)
	ON_COMMAND(ID_CHECK_MODEL_REMOVE, &CCheckModelLibraryDialog::OnCheckModelRemove)
	ON_COMMAND(ID_CHECK_MODEL_UPDATE, &CCheckModelLibraryDialog::OnCheckModelUpdate)
	ON_NOTIFY(NM_CLICK, IDC_LIST_MODEL, &CCheckModelLibraryDialog::OnNMClickListModel)
	ON_COMMAND(ID_CHECK_MODEL_EDIT, &CCheckModelLibraryDialog::OnCheckModelEdit)
	ON_UPDATE_COMMAND_UI(ID_CHECK_MODEL_EDIT, &CCheckModelLibraryDialog::OnUpdateCheckModelEdit)
END_MESSAGE_MAP()


// CCheckModelLibraryDialog 消息处理程序


BOOL CCheckModelLibraryDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	CBitmap bmp;
	if (bmp.LoadBitmap(IDB_BITMAP_MODEL_LIST_32))
	{
		m_il.Create(32, 32, ILC_COLOR32 | ILC_MASK, 0, 0);
		m_il.Add(&bmp, RGB(255, 0, 255));
		int nCount = m_il.GetImageCount();
		for (int i = 1; i < nCount; i++)         //support 16
		{
			m_il.SetOverlayImage(i, i);
		}
		m_list.SetImageList(&m_il, TVSIL_NORMAL);
	}

	m_list.Populate();

	CRect rect;
	GetDlgItem(IDC_STATIC_BORDER)->GetWindowRect(rect);
	ScreenToClient(rect);
	int margin = 1;
	rect.left += margin;
	rect.right -= margin;
	rect.top += margin;
	rect.bottom -= margin;
	m_grid.MoveWindow(rect);

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CCheckModelLibraryDialog::OnCheckModelNew()
{
	// TODO: 在此添加命令处理程序代码
	CCheckModelNewDialog dlg;
	if (dlg.DoModal()==IDOK)
	{
		m_list.Populate();
	}
}


void CCheckModelLibraryDialog::OnCheckModelRemove()
{
	// TODO: 在此添加命令处理程序代码
	AfxMessageBox("OnCheckModelRemove");
}


void CCheckModelLibraryDialog::OnCheckModelUpdate()
{
	// TODO: 在此添加命令处理程序代码
	AfxMessageBox("OnCheckModelUpdate");
}


void CCheckModelLibraryDialog::OnNMClickListModel(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码

	int nItem = pNMItemActivate->iItem;
	if (nItem < 0)
	{
		return;
	}

	
	z_uint32 code = (z_uint32)m_list.GetItemData(nItem);
	CString strModName = m_list.GetItemText(nItem,0);

	ck::CheckModel* pModel = NULL;
	ck::CheckModelManager* pModelManager = ck::CheckModelManager::GetInstance();
	pModel = pModelManager->GetModel(strModName);
	if (pModel)
	{
		m_grid.Populate(pModel);
		pModel->Release();
	}

	*pResult = 0;
}


void CCheckModelLibraryDialog::OnCheckModelEdit()
{
	// TODO: 在此添加命令处理程序代码
	POSITION pos = m_list.GetFirstSelectedItemPosition();
	if (pos)
	{
		int nItem = m_list.GetNextSelectedItem(pos);

		CString strModName = m_list.GetItemText(nItem, 0);
		ck::CheckModel* pCheckModel = NULL;
		ck::CheckModelManager* pModelManager = ck::CheckModelManager::GetInstance();
		pCheckModel = pModelManager->GetModel(strModName);
		if (pCheckModel)
		{
			CCheckModelEditDialog dlg;
			dlg.SetModel(pCheckModel);
			dlg.DoModal();
		}
	}
}


void CCheckModelLibraryDialog::OnUpdateCheckModelEdit(CCmdUI *pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
}
